﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace GW2LastOnline.Controls
{
    class ImageButton : Control
    {
        private enum ButtonState { Normal = 1, Pressed = 2, Active = 4, Fixed = 8 };
        private ButtonState state;

        private Image imageFixed;

        public ImageButton()
            : base()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            state = ButtonState.Normal;
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);

            AddState(ButtonState.Active);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            RemoveState(ButtonState.Active);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            AddState(ButtonState.Pressed);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            RemoveState(ButtonState.Pressed);
        }

        private void AddState(ButtonState state)
        {
            if (!HasState(state))
                SetState(this.state | state);
        }

        private void RemoveState(ButtonState state)
        {
            if (HasState(state))
                SetState(this.state ^ state);
        }

        private bool HasState(ButtonState state)
        {
            return (this.state & state) == state;
        }

        public Image Image
        {
            get;
            set;
        }

        public Image ImageActive
        {
            get;
            set;
        }

        public Image ImagePressed
        {
            get;
            set;
        }

        public Image ImageFixed
        {
            get
            {
                return imageFixed;
            }
            set
            {
                imageFixed = value;

                if (value == null)
                {
                    RemoveState(ButtonState.Fixed);
                }
                else
                {
                    if (HasState(ButtonState.Fixed))
                        this.Invalidate();
                    else
                        AddState(ButtonState.Fixed);
                }
            }
        }

        private void SetState(ButtonState state)
        {
            if (this.state == state)
                return;

            this.state = state;

            this.Invalidate();
        }

        private Image GetImage()
        {
            Image i = null;

            ButtonState state = this.state;
            int s = (int)state;

            if (HasState(ButtonState.Fixed))
            {
                i = this.ImageFixed;
            }
            else if (HasState(ButtonState.Pressed))
            {
                i = this.ImagePressed;
            }
            else if (HasState(ButtonState.Active))
            {
                i = this.ImageActive;
            }
            else
            {
                i = this.Image;
            }

            if (i == null)
                i = this.Image;

            return i;
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            Graphics g = pevent.Graphics;
            Image i = GetImage();

            if (i != null)
                g.DrawImageUnscaled(i, 0, 0);

        }
    }
}
